const db = uniCloud.database();
const dbCmd = db.command;
const { chapterNameSplitStr } = require('../../config/index.js');
module.exports = async function(options = {}) {
	const { groupCode, memberUid, characterId } = options;
	const token = this.getUniIdToken();
	if (!groupCode || !memberUid || !characterId) {
		throw new Error('非法访问');
	}
	const [groupList] = await Promise.all([
		db.collection('a-dz-group-list').where({ groupCode }).get({ getOne: true })
	])
	if (groupList.data.length) { //保证创建角色才能修改
		const group = groupList.data[0];
		if (group.characterId !== characterId) {
			throw new Error('非法操作');
		}
		const group_chapters_custom = group.chapters_custom.map(item => `${groupCode}${chapterNameSplitStr}${item}`);
		await Promise.all([
			db.collection('a-dz-group-apply-list').where({ groupCode, uid: memberUid }).remove(),
			db.collection('uni-id-users').doc(memberUid).update({
				chapters_custom: dbCmd.pullAll(group_chapters_custom)
			})
		])
		return {
			code: 0,
			msg: '移除成功'
		}
	} else {
		throw new Error('非法访问');
	}
}